import 'package:flutter_blog_manage_study/base/get/controller/base_get_controller.dart';
import 'package:flutter_blog_manage_study/res/strings.dart';
import 'package:flutter_blog_manage_study/routes/routes.dart';
import 'package:flutter_blog_manage_study/util/toast_util.dart';
import 'package:get/get.dart';

/// * 作者: Tao
/// * 日期: 2024/3/22
/// * 时间: 14:19
/// * 描述: 登录 controller
class LoginController extends BaseGetController {
  /// 账户
  String account = "";

  /// 密码
  String password = "";

  /// 当前按钮是否可点击
  bool changeShowButton() {
    return account.isNotEmpty && password.isNotEmpty;
  }

  /// 用户登录
  void login() {
    if (account.isEmpty || password.isEmpty) {
      return;
    }

    /// 账户名：>6位
    if (account.isEmpty || account.length < 6) {
      ToastUtil.show(account.isEmpty
          ? Strings.registerAccountEmpty.tr
          : Strings.registerAccountLength.tr);
      return;
    }

    /// 密码：>6位
    if (password.isEmpty || password.length < 6) {
      ToastUtil.show(password.isEmpty
          ? Strings.registerPasswordEmpty.tr
          : Strings.registerPasswordLength.tr);
      return;
    }

    request.login(account, password, success: (data) {
      ToastUtil.show(Strings.loginSuccess.tr);
      Get.offAllNamed(Routes.homePage);
    });
  }
}
